home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / programs / graphics / gif2rpc / source / 8bpp / c / sierra2_4a < prev    next >
Encoding:
Text File  |  1995-10-16  |  3.5 KB  |  119 lines

  1. /* sierra2_4a.c
  2.  * AUTHOR:      Cy Booker, cy@cheepnis.demon.co.uk
  3.  * LICENSE:     FreeWare, Copyright (c) 1995 Cy Booker
  4.  *
  5.  * filter:              *  2
  6.  *                   1  1               (1/4)
  7.  */
  8.  
  9. #include "internal.h"
  10.  
  11. #include <assert.h>
  12. #include <string.h>             /* memset() */
  13. #include <stdlib.h>             /* calloc() */
  14.  
  15. #include "OS:hourglass.h"
  16. #include "OS:macros.h"
  17.  
  18.  
  19.  
  20. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  21.  */
  22.  
  23. extern bool process_gif_8bpp_sierra2_4a(
  24.                 const process_gif       *p) {
  25.   byte                  *rove;
  26.   int                   width, height;
  27.   int                   x, y;
  28.   const os_colour       *palette;
  29.   int                   line_length;
  30.   int                   *buffer, *this_row, *next_row;
  31.   int                   buffer_width;
  32.   int                   red, grn, blu;
  33.   os_colour             colour;
  34.   int                   error_red, error_grn, error_blu;
  35.   int                   temp_red, temp_grn, temp_blu;
  36.   rgbtupleout           error;
  37.   rgbtupleout_fn        fn;
  38.  
  39.   assert(p);
  40.   assert(p->pixel_width > 0);
  41.   assert(p->pixel_height > 0);
  42.   assert(p->in_palette.colours);
  43.   assert(p->fn);
  44.  
  45.   /*
  46.    * sierra2_4a requires storing error info for one pixel to left
  47.    * so we will just allocate one extra column for each row and not do any edge checks
  48.    * each entry is stored as {r*SCALE, g*SCALE, b*SCALE}
  49.    */
  50.   buffer_width = (p->pixel_width + 1) * 3;
  51.   buffer = calloc(1, sizeof(*buffer) * (buffer_width * 2));
  52.   if (!buffer) {
  53.     /*
  54.      * oh dear
  55.      */
  56.     return TRUE;
  57.   }
  58.  
  59.   error.palette = p->out_palette;               /* structure copy */
  60.   /*
  61.    * not we pre-load values from the process_gif array
  62.    * because it considerably helps the compiler produce better code
  63.    */
  64.   rove = p->buffer;
  65.   width = p->pixel_width;
  66.   height = p->pixel_height;
  67.   palette = p->in_palette.colours;
  68.   line_length = p->line_length;
  69.   fn = p->fn;
  70.   for (y= height; (y > 0); y--) {
  71.     if ((y & 7) == 0) {
  72.       xhourglass_percentage((y * 100) / height);
  73.     }
  74.     this_row = buffer + (buffer_width * ((y + 2) % 2)) + 1*3;
  75.     next_row = buffer + (buffer_width * ((y + 1) % 2)) + 0*3;
  76.     /* bottom row has no errors */
  77.     memset(next_row, 0, sizeof(*next_row) * (buffer_width - 1*3));
  78.     error_red = error_grn = error_blu = 0;                      /* error along this row */
  79.     /*
  80.      * note that just because we are actually scanning/outputting right to left
  81.      * doesn't matter as far as sierra2_4a is concerned
  82.      * although it might help if we could ``snake''
  83.      */
  84.     for (x= width - 1; (x >= 0); x--) {
  85.       INPUT;
  86.  
  87.       red += *this_row++;                               /* add in errors from other row */
  88.       grn += *this_row++;
  89.       blu += *this_row++;
  90.  
  91.       red += error_red;                                 /* add in errors from this row */
  92.       grn += error_grn;
  93.       blu += error_blu;
  94.  
  95.       PROCESS;
  96.  
  97.       temp_red = red / 4;                               /* diffuse pixel `below left' */
  98.       temp_grn = grn / 4;
  99.       temp_blu = blu / 4;
  100.       *next_row += temp_red; next_row++;
  101.       *next_row += temp_grn; next_row++;
  102.       *next_row += temp_blu; next_row++;
  103.  
  104.       next_row[0] += temp_red;                          /* diffuse pixel `below' */
  105.       next_row[1] += temp_grn;
  106.       next_row[2] += temp_blu;
  107.  
  108.       error_red = red - (temp_red * 2);                 /* diffuse pixel to `right' */
  109.       error_grn = grn - (temp_grn * 2);
  110.       error_blu = blu - (temp_blu * 2);
  111.     }
  112.     rove += line_length;
  113.   }
  114.   free(buffer);
  115.   return FALSE;
  116. }
  117.  
  118.  
  119.